import { useRequest as useVueRequest } from 'vue-request'
import axiosInstance from '@/utils/request'

// 通用 useRequest
export function useRequest(service, options = {}) {
  return useVueRequest(service, options)
}

// 上传文件
export function useUpload(url, file, formDataKey = 'file', extraData) {
  const formData = new FormData()
  formData.append(formDataKey, file)
  if (extraData) {
    Object.entries(extraData).forEach(([key, value]) => {
      formData.append(key, value)
    })
  }
  return useRequest(() =>
    axiosInstance({
      url,
      method: 'POST',
      data: formData,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  )
}

// 快捷方法封装（run/runAsync 参数就是请求体或查询参数）
export function useGet(url, options = {}) {
  return useRequest(
    params => axiosInstance({ url, method: 'GET', params, ...(options.axiosConfig || {}) }),
    options
  )
}

export function usePost(url, options = {}) {
  return useRequest(
    data => axiosInstance({ url, method: 'POST', data, ...(options.axiosConfig || {}) }),
    options
  )
}

export function usePut(url, options = {}) {
  return useRequest(
    data => axiosInstance({ url, method: 'PUT', data, ...(options.axiosConfig || {}) }),
    options
  )
}

export function useDelete(url, options = {}) {
  return useRequest(
    params => axiosInstance({ url, method: 'DELETE', params, ...(options.axiosConfig || {}) }),
    options
  )
}
